package org.example.BinaryTree;

public class Total_Path {
//    public static int sum = 0;
    public static boolean flag = false;
    public static void main(String[] args) {
        //路径总和

        //给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径，这条路径上所有节点值相加等于目标和 targetSum 。如果存在，返回 true ；否则，返回 false 。
        //叶子节点 是指没有子节点的节点。
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode root = new TreeNode(0);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        node5.left = node7;
        get(root,14,0);
        System.out.println(flag);
    }
    public static void get(TreeNode node, int targetSum,int sum){
        if (node == null || flag) return;
        sum+= node.val;
        if (node.right==null && node.left == null && sum == targetSum) flag=true;
        get(node.left,targetSum,sum);
        get(node.right,targetSum,sum);
    }
}
